/***************************************************************************
 * PacketCombiner
 * Combine several packet into one using RSTP protocol
 * Note: 
 *	1. single-thread safe
 *	2. re-use is denied
 * Author	: song bo
 * Date		: 2006-10-18
 * Project	: P2P
 **************************************************************************/

#ifndef _PACKETCOMBINER_H_
#define _PACKETCOMBINER_H_

#ifdef WIN32
#pragma warning(disable:4786)
#endif

#include "OSHeaders.h"
#include <map>


class CPacketCombiner
{
private:
	struct SData
	{
		char *		pData;
		UInt16		Len;

		SData()
		{
			pData = NULL;
			Len = 0;
		}
		~SData()
		{
			if(pData)
				delete[] pData;
		}
	};
	typedef std::map<UInt16,SData*>	DataMap;
	typedef DataMap::iterator	DataMapIterator;

public:
public:
	CPacketCombiner();
	~CPacketCombiner();

public:
	/*
	 * INPUT:
	 * OUTPUT:
	 * RETURN:
         */
	void	Initialize(UInt8 inChanID, UInt16 inRequest);
	/*
	 * copy data into internal buffer and check if all data are recved
	 * INPUT:
	 * OUTPUT:
	 * RETURN:
	 *		INPUT_FINISH: all data arrived
	 *		INPUT_OK: current data has been saved
	 *		INPUT_ERR: fetal error
         */
	enum INPUT_RET{ INPUT_OK= 0, INPUT_DUP, INPUT_ERR, INPUT_FINISH};
	INPUT_RET	InputData(char *pBuff, UInt16 inLen);

	/*
	 * get current received data length
         */
	UInt32	GetRecvDataLen();

	/*
	 * dump data into buffer
	 * INPUT:
	 *	inBuffLen: make sure it is above actual received data length
	 * OUTPUT:
	 *	outLen: act data len
	 * RETURN:
	 *	OS_NoErr: dump ok
	 *	OS_Err: dump failed
         */
	Int32	DumpData(char *pBuff, UInt32 inBuffLen, UInt32 *outLen);

private:
	UInt32		m_RecvDataLen;
	DataMap		m_mapData;

	UInt8		m_ChanID;
	UInt16		m_RequestID;
};
#endif	//_PACKETCOMBINER_H_

